package com.yz.group1.group_1.controller;

import com.yz.group1.group_1.entity.User;
import com.yz.group1.group_1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin // 支持跨域请求
@RestController
@RequestMapping("/user") // API 请求路径前缀
public class UserController {

    @Autowired
    private UserService userService;

    // 获取所有用户列表
    @GetMapping
    public List<User> userList() {
        return userService.listAllUsers();
    }

    // 根据ID获取用户
    @GetMapping("/{id}")
    public User getUserById(@PathVariable Integer id) {
        return userService.getById(id);
    }

    // 移除新增用户接口

    // 更新用户信息
    @PostMapping("/update")
    public boolean updateUser(@RequestBody User user) {
        return userService.updateById(user);
    }

    // 更新用户状态（冻结/解冻）
    @PostMapping("/updateStatus")
    public boolean updateStatus(@RequestBody User user) {
        User entity = new User();
        entity.setId(user.getId());
        entity.setStatus(user.getStatus());
        return userService.updateById(entity);
    }

    // 删除用户
    @DeleteMapping("/delete/{id}")
    public boolean deleteUser(@PathVariable Integer id) {
        return userService.removeById(id);
    }
}